package com.nw.dsl4j.grammar;

import java.util.ArrayList;
import java.util.List;

import com.nw.dsl4j.annotations.Cardinality;
import com.nw.dsl4j.annotations.CardinalityType;
import com.nw.dsl4j.annotations.Rule;
import com.nw.dsl4j.annotations.TokenSeperatedCollection;

@Rule(pattern = "${identifiers}")
public class Fqn {

	@Cardinality(cardinalityType = CardinalityType.OneOrMore)
	@TokenSeperatedCollection(token = DOT.class)
	List<ID> identifiers = new ArrayList<ID>();

	public List<ID> getIdentifiers() {
		return identifiers;
	}

	public void setIdentifiers(List<ID> identifiers) {
		this.identifiers = identifiers;
	}
	
	@Override
	public boolean equals(Object obj) {
		Fqn fqn = (Fqn)obj;
		return this.toString().equals(fqn.toString());
	}
	@Override
	public int hashCode() {
		return this.toString().hashCode();
	}
	
	public Fqn derive(ID id) {
		Fqn ret = new Fqn();
		ret.getIdentifiers().addAll(getIdentifiers());
		ret.getIdentifiers().add(id);
		return ret;
	}
	
	@Override
	public String toString() {
		String ret = "";
		for(int i = 0 ; i < identifiers.size() ; i++) {
			if(i>0) {
				ret+=".";
			}
			ret+=identifiers.get(i).toString();
		}
		return ret;
	}

	public Fqn trimRoot() {
		Fqn ret = new Fqn();
		ret.getIdentifiers().addAll(getIdentifiers());
		ret.getIdentifiers().remove(0);
		return ret;
	}

	public Fqn derive(Fqn fqn) {
		Fqn ret = new Fqn();
		ret.getIdentifiers().addAll(getIdentifiers());
		ret.getIdentifiers().addAll(fqn.getIdentifiers());
		return ret;
	}
}
